package org.Raven.domain.strategy.service.rule.tree.impl;

import lombok.extern.slf4j.Slf4j;
import org.Raven.domain.strategy.model.valobj.RuleLogicCheckTypeVO;
import org.Raven.domain.strategy.model.valobj.StrategyAwardStockKeyVO;
import org.Raven.domain.strategy.repository.IStrategyRepository;
import org.Raven.domain.strategy.service.armory.IStrategyDispatch;
import org.Raven.domain.strategy.service.rule.tree.ILogicTreeNode;
import org.Raven.domain.strategy.service.rule.tree.factory.DefaultTreeFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Description 库存节点
 * @Author Raven
 * @Date 2025/1/25
 */
@Slf4j
@Component("rule_stock")
public class RuleStockLogicTreeNode implements ILogicTreeNode {
    @Resource
    private IStrategyDispatch strategyDispatch;
    @Resource
    private IStrategyRepository strategyRepository;
    @Override
    public DefaultTreeFactory.TreeActionEntity logic(String userId, Long strategyId, Integer awardId,String ruleValue, Date endDateTime) {
        log.info("规则过滤-库存扣减 userId:{} strategyId:{} awardId:{}",userId,strategyId,awardId);
        //扣减库存
        Boolean status = strategyDispatch.subtractionAwardStock(strategyId, awardId,endDateTime);
        //判读status ture成功 false失败
        if(status){
            //写入延时队列，延时消费更新数据库记录  【在trigger的job;】
            strategyRepository.awardStockConsumeSendQueue(StrategyAwardStockKeyVO.builder()
                    .strategyId(strategyId)
                    .awardId(awardId)
                    .build());
            return DefaultTreeFactory.TreeActionEntity.builder()
                    .ruleLogicCheckType(RuleLogicCheckTypeVO.TAKE_OVER)
                    .strategyAwardVO(DefaultTreeFactory.StrategyAwardVO.builder()
                            .awardId(awardId)
                            .awardRuleValue("ruleValue")
                            .build())
                    .build();
        }


        return DefaultTreeFactory.TreeActionEntity.builder()
                .ruleLogicCheckType(RuleLogicCheckTypeVO.ALLOW)
                .build();
    }

}
